import json
from flask import current_app, Blueprint, render_template, request
from app import db

module_tx_prod_vol = Blueprint('tie_xi_production_volume', __name__, template_folder='templates')


@module_tx_prod_vol.route('/')
def production_volume():
    return render_template('ProductionVolume.html')


@module_tx_prod_vol.route('/PressShopInputPage')
def press_shop_input():
    return render_template('PressShopInputPage.html')


@module_tx_prod_vol.route('/api/production_volume')
def api_for_production_volume():
    db_engine = db.get_engine(current_app, bind='IPS_T_Tiexi_Online')

    assembly_part = "'A821OEE_F0_Control','A821OEE_F1_Control','A821OEE_Monthly_F1','A821TLG1._PLC.TVI.Z001'," \
                    "'A.CVI.Z067','A.CVI.Z008','A.CVI.Z013','A.CVI.Z007','A.CVI.Z066','A821OEE_F1_cur','A.CVI.Z015'," \
                    "'A821OEE_F1_diff','A.CVI.Z061','A821OEE_F2_cur','A.CVI.Z027','A821OEE_F2_diff'," \
                    "'apipso72.OSM022','apipso72.OSM032','apipso72.OSM058','apipso72.OSM064','apipso72.OSM029'," \
                    "'A.CVI.Z081','A.CVI.Z084','apipso72.OSM168','apipso72.OSM169','apipso72.OSM170'," \
                    "'apipso72.OSM171','apipso72.OSM172', 'A821OEE_F0_Hourly Output Control', 'A821OEE_F1_Hourly Output Control'," \
                    "'A821OEE_F2_Hourly Output','A821OEE_Monthly_Hourly Output'"
    paint_part = "'apipsp72.PPE128','apipsp72.PPE129','apipsp72.PPE130','319101Z1._PLC.CVI.M0002','319101Z1._PLC.CVI.M0105','P_Conveyor.P_in_daily_target'," \
                 "'P_Conveyor.P_out_daily_target','P_Conveyor.P_in_Cur_target','P_Conveyor.P_out_cur_target','319116Z1._PLC.CVI.M0075'," \
                 "'apipsp72.PPE1208','apipso72.EVO358'"
    body_shop_part = "'3_Daily_Target_LK_UB_F30','3_Daily_Target_LK_UB_F35','3_LK_UB_F30_CurrentTargetDay'," \
                     "'3_LK_UB_F35_CurrentTargetDay','3_LK_UB_F30_ActualDay','3_LK_UB_F35_ActualDay'," \
                     "'3_Daily_Target_F45_GM','3_Daily_Target_F52_GM','3_Daily_Target_F49_GM'," \
                     "'3_F45_GM_CurrentTargetDay','3_F52_GM_CurrentTargetDay','3_F49_GM_CurrentTargetDay'," \
                     "'3_LU_UB_F45_ActualDay','3_LU_UB_F52_ActualDay','3_LU_UB_F49_ActualDay'," \
                     "'3_LK_FR3_F30_ActualDay','3_LK_FR3_F35_ActualDay','3_Daily_Target_F49P_GM'," \
                     "'3_F49P_GM_CurrentTargetDay','3_LU_FR3_F45_ActualDay','3_LU_FR3_F52_ActualDay'," \
                     "'3_LU_FR3_F49_ActualDay','3_LU_FR3_F49P_ActualDay','3_Daily_Target_F30_GM'," \
                     "'3_Daily_Target_F35_GM','3_F30_GM_CurrentTargetDay','3_F35_GM_CurrentTargetDay'," \
                     "'3_F30_ActualDay','3_F35_ActualDay','3_Daily_Target_F45_GM','3_Daily_Target_F52_GM'," \
                     "'3_Daily_Target_F49_GM','3_Daily_Target_M13_GM','3_M13_GM_CurrentTargetDay'," \
                     "'3_MF_F45_ActualDay','3_MF_F52_ActualDay','3_MF_F49_ActualDay','3_MF_F49P_ActualDay'," \
                     "'3_MF_M13_ActualDay','3_Daily_Target_BIW_Total','3_BIW_Total_CurrentTargetDay'," \
                     "'3_BIW_Total_ActualDay'"
    sql = "SELECT value_symbol,value_num,value_str FROM p_values WHERE value_symbol IN (%s, %s, %s)" % \
          (assembly_part, paint_part, body_shop_part)
    query_data = db_engine.execute(sql)
    data_dict = {}
    new_list = ['apipso72.OSM168', 'apipso72.OSM169', 'apipso72.OSM170', 'apipso72.OSM171', 'apipso72.OSM172']
    for row in query_data:
        idx = row[0]
        if idx in new_list:
            val = row[2]
        else:
            val = row[1]
        data_dict[idx] = val
    return json.dumps(data_dict)
